var category_type = null;
var progect_type = null;
var project_hivetype = null;
var project_state = null;
var voltage_type = null;

var orgall = null;//所有人员
var userall = null;//所有单位
var userallmy = null;
$(function () {
    $.when(getdatadictionary("category_type"),
        getdatadictionary("progect_type"),
        getdatadictionary("project_hivetype"),
        getdatadictionary("check_project_type"),
        getdatadictionary("voltage_type"),

        getorgall(),
        getuserall()).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'eys/epbase/list?state=isdelno&ourid=isok',
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                {label: '所属批次ID', name: 'bGuid', index: 'b_guid', width: 80, hidden: true},
                {
                    label: '业务类型', name: 'pBCategory', index: 'p_b_category', width: 80, hidden: true,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in category_type) {
                            if (cellvalue == category_type[d].dKey) return category_type[d].dValue;
                        }
                    }
                },
                {
                    hidden: true, label: '项目类别', name: 'pBType', index: 'p_b_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in progect_type) {
                            if (cellvalue == progect_type[d].dKey) return progect_type[d].dValue;
                        }
                    }
                },
                {
                    label: '储备状态', name: 'pBHive', index: 'p_b_hive', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in project_hivetype) {
                            if (cellvalue == project_hivetype[d].dKey) return project_hivetype[d].dValue;
                        }
                    }
                },
                {
                    label: '项目状态', name: 'pBState', index: 'p_b_state', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in project_state) {
                            if (cellvalue == project_state[d].dKey) return project_state[d].dValue;
                        }
                    }
                },
                {label: '项目名称', name: 'pBName', index: 'p_b_name', width: 200},
                {label: '项目地址', name: 'pBAddress', index: 'p_b_address', width: 80, hidden: true},
                {label: '项目经度', name: 'pBLongitude', index: 'p_b_longitude', width: 80, hidden: true},
                {label: '项目纬度', name: 'pBLatitude', index: 'p_b_latitude', width: 80, hidden: true},
                {
                    label: '项目单位', name: 'pBOrgid', index: 'p_b_orgid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {

                        for (d in orgall) {
                            if (cellvalue == orgall[d].deptId)
                                return orgall[d].name;
                        }
                    }
                },

                {
                    hidden: true, label: '最高电压等级', name: 'pBVoltage', index: 'p_b_voltage', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                        }
                    }
                },
                {
                    label: '验收人', name: 'pBCollectid', index: 'p_b_collectid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in userall) {
                            var ss = (cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "") + "");
                            for (var i = 0; i < ss.length; i++) {
                                if (ss[i] == userall[d].userId + "") {
                                    names += userall[d].username + ",";
                                }
                            }


                        }
                        if (names.length > 0) {
                            names = names.substring(0, names.length - 1);
                        }
                        return names;
                    }
                },

                {
                    label: '验收时间', name: 'pBAddtime', index: 'p_b_addtime', width: 80,
                    formatter: function (value, options, row) {
                        return value != null ? new Date(value).Format('yyyy-MM-dd hh:mm') : "";
                    }
                },
                {
                    label: '问题数量', name: 'problemCount', index: 'problemCount', width: 80,
                },
                {
                    label: '操作', name: '', width: 200, formatter: function (cellvalue, options, rowObject) {

                        if(rowObject.pBType == 100 && rowObject.isDraw=="是") {
                            return "<a  class='btn btn-primary' onclick='showcj(\""
                                + rowObject.guid + "\",\"" + rowObject.pBLongitude + "\" ,\"" + rowObject.pBLatitude + "\")' >&nbsp;项目信息</a>"
                                + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='guidang(\""
                                + rowObject.guid + "\")' >&nbsp;归档</a>"
                                + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='showDXF(\"" + rowObject.guid + "\",\"" + rowObject.pBName + "\")' >&nbsp;单线图</a>"
                        }else{
                            return "<a  class='btn btn-primary' onclick='showcj(\""
                                + rowObject.guid + "\",\"" + rowObject.pBLongitude + "\" ,\"" + rowObject.pBLatitude + "\")' >&nbsp;项目信息</a>"
                                + "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='guidang(\""
                                + rowObject.guid + "\")' >&nbsp;归档</a>"
                        }
                }
                }

            ],
            viewrecords: true,
            height: 500,
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    )
});
function guidang(guid) {
    layer.confirm('您是否要归档该项目？', {
        btn: ['取消', '归档'] //按钮
    }, function () {
        layer.closeAll();
    }, function () {
        $.get(baseURL + "eys/epbase/guidang/" + guid, function (r) {
            if (r.code === 0) {
                alert('操作成功', function (index) {
                    vm.reload();
                });
            } else {
                alert(r.msg);
            }
        });
    });
}
function showcj(guid, longitude, latitude) {
    var title = "采集信息";
    var content = "epbaseothorcj.html?guid=" + guid + "&longitude=" + longitude + "&latitude=" + latitude;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['95%', '95%'],
        content: content

    });
}


/**
 * 跳转到查看单线图页面
 *
 * @param guid -- 项目ID和项目名称
 * 2018-11-19 10:49:09(新版)
 */
function showDXF(guid,pBName) {

    var title ="单线图";
    // var pGuid = vm.q.pGuid;
    var content = "eshowdxf.html?guid=" + guid+"&pBName="+pBName;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: content
    });
}


function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "category_type") {
            category_type = r.returnlist;
        }
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
        if (sss == "project_hivetype") {
            project_hivetype = r.returnlist;
        }
        if (sss == "check_project_type") {
            project_state = r.returnlist;
        }
        if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }
        vm.category_type = category_type;
        vm.progect_type = progect_type;
        vm.project_hivetype = project_hivetype;
        vm.project_state = project_state;
        vm.voltage_type = voltage_type;


    });
}
function getorgall() {
    $.get(baseURL + "sys/dept/selectall", function (r) {
        orgall = r.returnlist;
    });
}
function getuserall() {
    $.get(baseURL + "sys/user/select", function (r) {
        userall = r.returnlist;
        userallmy = r.myuserlist;
    });
    vm.userall = userall;
    vm.userallmy = userallmy;
}


var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            pBType: null,
            pBHive: null,
            pBName: null,
            pBState: null
        },
        orgall: orgall,
        userall: userall,
        userallmy: userallmy,

        showList: true,
        title: null,
        ePBase: {pBCollectid: []},
        status: ["是", "否"],

        category_type: category_type,
        progect_type: progect_type,
        project_hivetype: project_hivetype,
        project_state: project_state,
        voltage_type: voltage_type

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.ePBase = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.ePBase.guid == null ? "eys/epbase/save" : "eys/epbase/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ePBase),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "eys/epbase/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "eys/epbase/info/" + guid, function (r) {
                vm.ePBase = r.ePBase;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'pBType': vm.q.pBType,
                    'pBHive': vm.q.pBHive,
                    'pBName': vm.q.pBName,
                    'pBState': vm.q.pBState

                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});